View Javadoc
1   package org.apache.maven.surefire.junitcore.pc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.report.ConsoleStream;
23  
24  import java.util.Queue;
25  import java.util.concurrent.ConcurrentLinkedQueue;
26  
27  /**
28   * The sequentially executing strategy in private package.
29   *
30   * @author Tibor Digana (tibor17)
31   * @see SchedulingStrategy
32   * @since 2.16
33   */
34  final class InvokerStrategy
35      extends SchedulingStrategy
36  {
37  
38      private final Queue<Thread> activeThreads = new ConcurrentLinkedQueue<Thread>();
39  
40      protected InvokerStrategy( ConsoleStream logger )
41      {
42          super( logger );
43      }
44  
45      @Override
46      public void schedule( Runnable task )
47      {
48          if ( canSchedule() )
49          {
50              final Thread currentThread = Thread.currentThread();
51              try
52              {
53                  activeThreads.add( currentThread );
54                  task.run();
55              }
56              finally
57              {
58                  activeThreads.remove( currentThread );
59              }
60          }
61      }
62  
63      @Override
64      protected boolean stop()
65      {
66          return disable();
67      }
68  
69      @Override
70      protected boolean stopNow()
71      {
72          final boolean stopped = disable();
73  
74          for ( Thread activeThread = activeThreads.poll(); activeThread != null; activeThread = activeThreads.poll() )
75          {
76              activeThread.interrupt();
77          }
78          return stopped;
79      }
80  
81      @Override
82      public boolean hasSharedThreadPool()
83      {
84          return false;
85      }
86  
87      @Override
88      public boolean finished()
89          throws InterruptedException
90      {
91          return disable();
92      }
93  
94      public boolean destroy()
95      {
96          return stop();
97      }
98  }